//
//  HTTripDeatillSomeoneCommentView.m
//  HeartTravel
//
//  Created by lanou on 15/10/30.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTTripDeatillSomeoneCommentView.h"
#import "UIImageView+WebCache.h"
#import "HTTripDeattillCommentPhotoModel.h"
@implementation HTTripDeatillSomeoneCommentView

- (instancetype)initWithFrame:(CGRect)frame
{
   self =  [super initWithFrame:frame];
    
    if (self)
    {
        _icon = [[UIImageView alloc]initWithFrame:CGRectMake(20, 15, 40, 40)];
        _icon.layer.cornerRadius = 20;
        _icon.layer.masksToBounds = YES;
        [self addSubview:_icon];
        
        _namelabel = [[UILabel alloc]initWithFrame:CGRectMake(65, 25, 200, 20)];
        _namelabel.text = @"123";
        _namelabel.textColor = [UIColor colorWithRed:0.203 green:1.000 blue:0.972 alpha:1.000];
        [self addSubview:_namelabel];
        
        
      
        
        _timelabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 60, 100, 20)];
        _timelabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_timelabel];
        
    }
    return self;
}

- (void)setDeattillCommentMD:(HTTripDeattillCommentModel *)deattillCommentMD
{
    //内容的高度
    CGFloat height = [HTTripDeatillSomeoneCommentView heightForContent:deattillCommentMD.content];
    //图片的数据
    //间隔
    int interval = 5;
    //开始的x轴
    int startX = 20;
    //开始的y轴
    int startY = 65 + height + 10;
    //图片的大小
    int pictureSize = 100;
    
    [_icon sd_setImageWithURL:[NSURL URLWithString:deattillCommentMD.avatar]];
    _namelabel.text = deattillCommentMD.user_name;
    _timelabel.text = deattillCommentMD.create_date;
    
    if (deattillCommentMD.content.length > 0) {
    
    _contentLabel = [[UILabel alloc]init];
    
        
    _contentLabel.text = deattillCommentMD.content;
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont systemFontOfSize:14];
    _contentLabel.frame = CGRectMake(20, 70, 300, [HTTripDeatillSomeoneCommentView heightForContent:_contentLabel.text]);
        
    [self addSubview:_contentLabel];
        
    _timelabel.frame = CGRectMake(20, 60 +  height + 20, 100, 20);
        
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 15 + 40 + 15 + height +  20 + 20);
    }
    
    if (deattillCommentMD.status_photoArr.count > 0 && deattillCommentMD.status_photoArr.count <= 3 ) {
        
        for (int i = 0 ; i <= deattillCommentMD.status_photoArr.count - 1; i ++) {
            
            int x = i % 3;
            int y = i / 3;
            
            HTTripDeattillCommentPhotoModel *photoModel = [[HTTripDeattillCommentPhotoModel alloc]init];
            photoModel  = deattillCommentMD.status_photoArr[i];
            
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(startX + x * (pictureSize + interval) , startY + y * (pictureSize + interval), 100, 100)];
            [imageview sd_setImageWithURL:[NSURL URLWithString:photoModel.thumb_img_url]];
            [self addSubview:imageview];
            
            _timelabel.frame = CGRectMake(20, 60 +  height + 20 + pictureSize, 100, 20);
            self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 15 + 40 + 15 + height + pictureSize  + 20 + 20);
        }
     }else if (deattillCommentMD.status_photoArr.count > 3 && deattillCommentMD.status_photoArr.count <= 6)
     {
         
         
         for (int i = 0 ; i <= deattillCommentMD.status_photoArr.count - 1; i ++) {
             
             int x = i % 3;
             int y = i / 3;
             
             HTTripDeattillCommentPhotoModel *photoModel = [[HTTripDeattillCommentPhotoModel alloc]init];
             photoModel  = deattillCommentMD.status_photoArr[i];
             
             UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(startX + x * (pictureSize + interval) , startY + y * (pictureSize + interval), 100, 100)];
             [imageview sd_setImageWithURL:[NSURL URLWithString:photoModel.thumb_img_url]];
             [self addSubview:imageview];
             
         _timelabel.frame = CGRectMake(20, 60 +  height + 20 + pictureSize * 2 + 10, 100, 20);

         self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 15 + 40 + 15 + height + pictureSize * 2 + 20 + 20);
     }

  }
}

+ (CGFloat)heightForContent:(NSString *)string
{
  
    CGRect bounds = [string boundingRectWithSize:CGSizeMake(300, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    return bounds.size.height;
}

@end
